/***************************************************
 @Name :        1196kk0
 @Time :        2025-05-26
 @Email :       2232506563@qq.com
 @Comment :     连接WIFI与缓冲清理
 @FileName :    drv_WIFI_TCP.c
 @Version :     4.0
****************************************************/

#include "drv_WIFI_TCP.h"

/********* 全局变量 *********/
char USART3_Recv_Buff[USART3_RECV_BUFF_SIZE];       //接受数据字符串
uint16_t USART3_Recv_Index = 0;                     //字符串下标
uint16_t USART3_Recv_Flag = 0;                      //中断标志位
uint16_t USART3_TCP_Connect_Step = 0;               //连接步骤标志位

/***************************************
函数名：clear_USART3_Recv_Buff
函数功能：清空串口3接收缓存
输入参数：无
输出参数：无
其它：
****************************************/
void clear_USART3_Recv_Buff(void)
{
   USART3_Recv_Index = 0;
   memset(USART3_Recv_Buff , 0 , sizeof(USART3_Recv_Buff));
}

/***************************************
函数名：WIFI_Connect
函数功能：连接WIFI
输入参数：无
输出参数：无
其它：  中断接收"OK",将USART3_TCP_Connect_Step置位
        上一步骤完成后才进行下一步骤
****************************************/
void WIFI_Connect(void)
{
	clear_USART3_Recv_Buff();
    // USART_Send_str(USART3 , "AT+RESTORE\r\n" , strlen("AT+RESTORE\r\n"));//开始连接时恢复出场设置
    // delay_ms(3000);
    clear_USART3_Recv_Buff();
    while (1)
    {
        switch (USART3_TCP_Connect_Step)
        {
        case 0:
            USART_Send_str(USART3 , "AT\r\n" , strlen("AT\r\n"));
            while (USART3_Recv_Flag == 0);
            printf("%d\r\n" , USART3_TCP_Connect_Step);
            if (USART3_Recv_Flag == 1)
            {
                clear_USART3_Recv_Buff();
                USART3_Recv_Flag = 0;
                USART3_TCP_Connect_Step = 1;
                printf("%d\r\n" , USART3_TCP_Connect_Step);
            }
            break;
        case 1:
            USART_Send_str(USART3 , "AT+CWMODE=1\r\n" , strlen("AT+CWMODE=1\r\n"));
            while (USART3_Recv_Flag == 0);
            if (USART3_Recv_Flag == 1)
            {
                clear_USART3_Recv_Buff();
                USART3_Recv_Flag = 0;
                USART3_TCP_Connect_Step = 2;
                printf("%d\r\n" , USART3_TCP_Connect_Step);
            }
            break;
        case 2:
            USART_Send_str(USART3 , "AT+CIPMUX=0\r\n" , strlen("AT+CIPMUX=0\r\n"));
            while (USART3_Recv_Flag == 0);
            if (USART3_Recv_Flag == 1)
            {
                clear_USART3_Recv_Buff();
                USART3_Recv_Flag = 0;
                USART3_TCP_Connect_Step = 3;
                printf("%d\r\n" , USART3_TCP_Connect_Step);
            }
            break;
        case 3:
            // USART_Send_str(USART3 , "AT+CWJAP=\"wzl\",\"337845818\"\r\n" , strlen("AT+CWJAP=\"wzl\",\"337845818\"\r\n"));
			USART_Send_str(USART3 , "AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n" , strlen("AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n"));
            while (USART3_Recv_Flag == 0);
            if (USART3_Recv_Flag == 1)
            {
                printf("%d\r\n" , USART3_TCP_Connect_Step);
				clear_USART3_Recv_Buff();
				USART3_Recv_Flag = 0;
                USART3_TCP_Connect_Step = 4;
            }
            break;
        case 4:
            // USART_Send_str(USART3 , "AT+CIPSTART=\"TCP\",\"192.168.61.57\",8080\r\n"  , strlen("AT+CIPSTART=\"TCP\",\"192.168.61.57\",8080\r\n" ));
		    USART_Send_str(USART3 , "AT+CIPSTART=\"TCP\",\"192.168.91.237\",8080\r\n"  , strlen("AT+CIPSTART=\"TCP\",\"192.168.91.237\",8080\r\n"));
            while (USART3_Recv_Flag == 0);
            if (USART3_Recv_Flag == 1)
            {
                clear_USART3_Recv_Buff();
                USART3_Recv_Flag = 0;
                USART3_TCP_Connect_Step = 5;
                printf("%d\r\n" , USART3_TCP_Connect_Step);
            }    
            break;
        }
        if (USART3_TCP_Connect_Step == 5)
        {
            break;
        }
        if (USART3_Recv_Flag == 2)
        {
            USART3_TCP_Connect_Step = 0;
            USART3_Recv_Flag = 0;
            delay_ms(1000);
        }
    }
}


